<?php

namespace App\Http\Controllers;

use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Routing\Controller as BaseController;

abstract class Controller extends BaseController {
	use AuthorizesRequests, DispatchesJobs, ValidatesRequests;

	public function is_admin() {
		$token = session('token');
		if (!$token) {
			return false;
		}
		return isset($token['user_id']) ? $token : false;
	}

	public function is_hidden_admin() {
		$token = $this->is_admin();
		if ($token == false) {
			return false;
		}
		return $token['isHidden'];
	}

	public function is_system_admin() {
		$token = $this->is_admin();
		if ($token == false) {
			return false;
		}
		$user = User::find($token['user_id']);
		return $user->hasRole('Administrators');
	}

	public function response($data = '', $status = 200, $headers = []) {
		return response()->json($data, $status, $headers);
	}

	public function error($msg = '', $status = 400) {
		$headers = ['Content-Type' => 'application/json'];
		return $this->response($msg, $status, $headers);
	}

	public function warning($msg, $url = false) {
		$url = $url ? $url : HTTP_REFERER;
		$data = compact('msg', 'url');
		return view('common/warning', $data);
	}
}
